package com.jinzhi.oss.service.impl;

import com.jinzhi.common.base.CoreServiceImpl;
import com.jinzhi.common.component.oss.support.UploadServer;
import com.jinzhi.common.config.IFastConfig;
import com.jinzhi.common.service.DictService;
import com.jinzhi.common.utils.DateUtils;
import com.jinzhi.common.utils.FileType;
import com.jinzhi.common.utils.FileUtil;
import com.jinzhi.oss.dao.FileDao;
import com.jinzhi.oss.domain.FileDO;
import com.jinzhi.oss.service.FileService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Date;

/**
 * <pre>
 * </pre>
 *
 * <small> 2018年3月23日 | Aron</small>
 */
@Service
@AllArgsConstructor
public class FileServiceImpl extends CoreServiceImpl<FileDao, FileDO> implements FileService {

    @Autowired
    private IFastConfig huahaoConfig;
    private UploadServer uploader;
    @Autowired
    private DictService dictService;


    @Override
    public String upload(byte[] uploadBytes, String fileName) {
        //String url = uploader.upload(uploadBytes, FileNameUtils.getFileName(fileName, ifastConfig));
        String url = uploader.upload(uploadBytes, fileName);
        url = url.replace("\\","/");
        FileDO sysFile = new FileDO(FileType.fileType(fileName), url, new Date());
        super.insert(sysFile);
        return url;
    }

//    @Override
//    public String upload(byte[] uploadBytes, String fileName) {
//        fileName = fileName.substring(0, fileName.indexOf(".")) + "-" + System.currentTimeMillis() + fileName.substring(fileName.indexOf("."));
//        fileName = huahaoConfig.getProjectName() + "/" + DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN_8)
//                + "/" + fileName;
//        String url = uploader.upload(uploadBytes, fileName);
//        FileDO sysFile = new FileDO(FileType.fileType(fileName), url, new Date());
//        super.insert(sysFile);
//        return url;
//    }

    public String upload(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        fileName = fileName.substring(0, fileName.indexOf(".")) + "-" + System.currentTimeMillis() + fileName.substring(fileName.indexOf("."));
        String fileName_suffix = "open/" + huahaoConfig.getProjectName() + "/" + DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN_8) + "/";
        String local_path = "C:/file/";
        String project_url = "http://114.115.217.149:8899/";
        if (StringUtils.isBlank(local_path)){
            //默认值
            local_path = "C:/file/";
        }
        if (StringUtils.isBlank(project_url)){
            //默认值
            project_url = "http://localhost:8088/";
        }
        String filePath = local_path + fileName_suffix;
        FileUtil.judeDirExists(new File(filePath));

        String prefix = project_url + fileName_suffix;
        String url = prefix + fileName;
        try {
            FileUtil.uploadFile(file.getBytes(),filePath,fileName);
            FileDO sysFile = new FileDO(FileType.fileType(fileName), url, new Date());
            super.insert(sysFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return url;
    }


    private byte[] getBytes(File file) {
        byte[] buffer = null;
        try {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }
}
